package com.anlogic.sdk.bootimage.utils;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import com.anlogic.sdk.bootimage.model.BootGenModel;
import com.anlogic.sdk.toolpreference.utils.BootGenPrefsUtils;

public class BootGenPreferencesUtils {
	private static final String IS_NEW = "isNew";
	private static final String BIF_FILE = "bifFile";
	private static final String IMPORT_BIF_FILE = "importBifFile";
	
	private static IEclipsePreferences bootgenPrefs = BootGenPrefsUtils.getPreferences();

	public static IEclipsePreferences getPreferences() {
		return bootgenPrefs;
	}
	
	public static boolean getIsNew() {
		boolean isNew = bootgenPrefs.getBoolean(IS_NEW, true);
		return isNew;
	}
	
	public static void putIsNew(boolean isNew) {
		bootgenPrefs.putBoolean(IS_NEW, isNew);
	}
	
	public static String getBifFile() {
		return bootgenPrefs.get(BIF_FILE, "");
	}
	
	public static void putBifFile(String bifFile) {
		bootgenPrefs.put(BIF_FILE, bifFile);
	}
	
	public static String getImportBifFile() {
		return bootgenPrefs.get(IMPORT_BIF_FILE, "");
	}
	
	public static void putImportBifFile(String importBifFile) {
		bootgenPrefs.put(IMPORT_BIF_FILE, importBifFile);
	}
	
	public static BootGenModel loadBootgenModel() {
		BootGenModel model = new BootGenModel();
		try {
			boolean isNew = getIsNew();
			model.setNew(isNew);
			String bifFile = getImportBifFile();
			if (!bifFile.isEmpty()) {
				model.setBifFile(bifFile);
			}
			String importBifFile = getImportBifFile();
			if (!importBifFile.isEmpty()) {
				model.setImportBifFile(importBifFile);
			}
		} catch (Exception e) {
			model = new BootGenModel();
		}
		return model;
	}

	public static void savePreferences(BootGenModel model) {
		bootgenPrefs.putBoolean(IS_NEW, model.isNew());
		bootgenPrefs.put(BIF_FILE, model.getBifFile());
		bootgenPrefs.put(IMPORT_BIF_FILE, model.getImportBifFile());
		BootGenPrefsUtils.flush();
	}
}
